﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Data.Doc;
using Enterprise.Model.Doc;
namespace Enterprise.Service.Doc
{
    public class DocService
    {
        /// <summary>
        /// 文章内容显示
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public DocArticleEntity DocArticleDisp(int articleId)
        {
            DocData data = new DocData();
            return data.DocArticleDisp(articleId);
        }

        /// <summary>
        ///  数据列表
        /// </summary>
        /// <returns></returns>
        public List<DocArticleEntity> DocArticleList(int moduleId)
        {
            DocData data = new DocData();
            return data.DocArticleList(moduleId);
        }

        /// <summary>
        ///  数据列表
        /// </summary>
        /// <returns></returns>
        public List<DocArticleEntity> DocArticleList(string cmd)
        {
            DocData data = new DocData();
            return data.DocArticleList(cmd);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sType">查询类型</param>
        /// <param name="skey">关键字</param>
        /// <returns></returns>
        public List<DocArticleEntity> DocArticleSearchList(string sType, string skey)
        {
            string cmd = "Select * from DOC_ARTICLES where " + sType + " like '%" + skey + "%' order by PUBDATE desc";
            DocData data = new DocData();
            return data.DocArticleList(cmd);
        }

        /// <summary>
        /// 通知公告 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void DocArticleExecute(DocArticleEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into DOC_ARTICLES(ID,CLASSID,TITLE,CONTENTS,TITLERU,CONTENTSRU,AUTHOR,PUBDATE,ARTICLEFROM,HITS,KEYS,ATOP,APIC,PUBUSER,LASTEDITOR,RCVEDUSERS,PASSED,UPLOADFILES,ATUIJIAN) values(SEQ_DOC_ARTICLES.nextval," + entity.CLASSID + ",'" + entity.TITLE + "',:CONTENTS,'" + entity.TITLERU + "',:CONTENTSRU,'" + entity.AUTHOR + "'," + Utility.FormatOracleDate(entity.PUBDATE) + ",'" + entity.ARTICLEFROM + "'," + entity.HITS + ",'" + entity.KEYS + "'," + entity.ATOP + ",'" + entity.APIC + "'," + entity.PUBUSER + ",'" + entity.LASTEDITOR + "','" + entity.RCVEDUSERS + "'," + entity.PASSED + ",'" + entity.UPLOADFILES + "'," + entity.ATUIJIAN + ")";
                    break;
                case "Update":
                    cmd = "Update DOC_ARTICLES set TITLE='"+entity.TITLE+"',CONTENTS=:CONTENTS,TITLERU='"+entity.TITLERU+"',CONTENTSRU=:CONTENTSRU,AUTHOR='"+entity.AUTHOR+"',PUBDATE="+Utility.FormatOracleDate(entity.PUBDATE)+",ARTICLEFROM='"+entity.ARTICLEFROM+"',HITS="+entity.HITS+",KEYS='"+entity.KEYS+"',ATOP="+entity.ATOP+",APIC='"+entity.APIC+"',PUBUSER="+entity.PUBUSER+",LASTEDITOR='"+entity.LASTEDITOR+"',RCVEDUSERS='"+entity.RCVEDUSERS+"',PASSED="+entity.PASSED+",UPLOADFILES='"+entity.UPLOADFILES+"',ATUIJIAN="+entity.ATUIJIAN+" where ID=" + entity.ID;
                    break;
                case "Delete":
                    cmd = "Delete from DOC_ARTICLES where ID=" + entity.ID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                if (entity.DB_Option_Action == "Delete")
                {
                    ArticleExecuteBySql(cmd);
                }
                else
                {
                    ArticleExecuteBySqlCommand(cmd, "CONTENTS", entity.CONTENTS, "CONTENTSRU", entity.CONTENTSRU);
                }
            }
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ArticleExecuteBySql(string cmd)
        {
            DocData data = new DocData();
            data.ArticleExecute(cmd);
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ArticleExecuteBySqlCommand(string cmd, string item1, object item1Value, string item2, object item2Value)
        {
            DocData data = new DocData();
            data.ArticleExecute(cmd, item1, item1Value, item2, item2Value);
        }

        /// <summary>
        /// 判断用户对当前通知公告是否有签收权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DocQianshouCheck(DocArticleEntity entity)
        {
            bool rbool = false;
            if (string.IsNullOrEmpty(entity.RCVEDUSERS))
            {
                rbool = true;
            }
            else
            {
                string[] arr = entity.RCVEDUSERS.Split(',');
                bool _rbool = arr.Contains(Utility.Get_UserId.ToString());
                if (_rbool || entity.PUBUSER == Utility.Get_UserId)
                {
                    rbool = true;
                }
            }
            return rbool;
        }

        /// <summary>
        ///  按签收范围进行筛选可查看的通知公告
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<DocArticleEntity> DocQianshouList(List<DocArticleEntity> list)
        {
            //加入查看范围控制
            //签收范围以及发布者则可查看
            List<DocArticleEntity> reList = new List<DocArticleEntity>();
            foreach (DocArticleEntity model in list)
            {
                bool rbool = DocQianshouCheck(model);
                if (rbool)
                {
                    reList.Add(model);
                }
            }
            return reList;
        }

    }
}
